﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio.Device;

namespace BDPAutodetect.Device
{
    /// <summary>
    /// 设备类型描述表
    /// </summary>
    public class DeviceTypeTable
    {
        /// <summary>
        /// 静态变量
        /// </summary>
        public static List<DeviceTypeData> Items { get; private set; }

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static DeviceTypeTable()
        {
            Items = new List<DeviceTypeData>();
            InitCreateList();
        }

        /// <summary>
        /// 静态函数
        /// </summary>
        public static void InitCreateList()
        {
            if (Items == null || Items.Count <= 0) { Items = new List<DeviceTypeData>(); }
            Items = new List<DeviceTypeData>()
            {
                new DeviceTypeData(DeviceType.None, "None", "未知"),
                new DeviceTypeData(DeviceType.Customer, "Customer", "客户机端"),
                new DeviceTypeData(DeviceType.Electrical, "Electrical", "电压电流采样板端"),
                new DeviceTypeData(DeviceType.SensorSign, "SensorSign", "DIDO输入输出板端")
            };
        }

        /// <summary>
        /// [获取设备类型]
        /// [根据设备类型名称]
        /// </summary>
        /// <param name="argDeviceTypeName">设备类型名称</param>
        /// <returns>设备类型枚举</returns>
        public static DeviceType GetDeviceTypeByName(string argDeviceTypeName)
        {
            try
            {
                if (Items == null || Items.Count <= 0) { InitCreateList(); }
                DeviceTypeData? CTypeData = Items?.Where(v => v.DeviceTypeName == argDeviceTypeName).FirstOrDefault();
                return CTypeData != null ? CTypeData.DeviceType : DeviceType.None;
            }
            catch (Exception)
            {
                return DeviceType.None;
            }
        }

        /// <summary>
        /// [获取设备类型]
        /// [根据设备类型描述]
        /// </summary>
        /// <param name="argDeviceTypeCaption">设备类型描述</param>
        /// <returns>设备类型枚举</returns>
        public static DeviceType GetDeviceTypeByCaption(string argDeviceTypeCaption)
        {
            try
            {
                if (Items == null || Items.Count <= 0) { InitCreateList(); }
                DeviceTypeData? CTypeData = Items?.Where(v => v.DeviceTypeCaption == argDeviceTypeCaption).FirstOrDefault();
                return CTypeData != null ? CTypeData.DeviceType : DeviceType.None;
            }
            catch (Exception)
            {
                return DeviceType.None;
            }
        }

        /// <summary>
        /// [获取设备类型描述]
        /// [根据设备类型]
        /// </summary>
        /// <param name="argDeviceType">设备类型</param>
        /// <returns>设备类型描述</returns>
        public static string GetCaptionByDeviceType(DeviceType argDeviceType)
        {
            try
            {
                if (Items == null || Items.Count <= 0) { InitCreateList(); }
                DeviceTypeData? CTypeData = Items?.Where(v => v.DeviceType == argDeviceType).FirstOrDefault();
                return CTypeData != null ? CTypeData.DeviceTypeCaption : string.Empty;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public static DeviceTypeData? GetDeviceTypeData(DeviceType argDeviceType)
        {
            if (Items == null || Items.Count <= 0) { InitCreateList(); }
            return Items?.FirstOrDefault(v => v.DeviceType == argDeviceType);
        }
    }
}
